package com.gopay.scheduler.job;

import com.gopay.common.domain.cust.CustPersonInfo;
import com.gopay.common.domain.cust.CustUniqIdenRel;
import com.gopay.common.user.dao.CustPersonInfoQueryDAO;
import com.gopay.cron.service.CustUniqIdenRelService;
import com.gopay.scheduler.GopayBaseJob;
import net.sf.json.JSONArray;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import java.util.*;
/**
 * CLASSNAME:AutoGenGenenatorUniqIdenForCustPerson
 * Description:自动生成唯一标识给新增的个人用户
 * DATE:2018/6/20/020
 * AUTHER:PANSHICHAO
 * www.gopay.com.cn Inc.All rights reserved.
 */
public class AutoGenGenenatorUniqIdenForCustPersonJob  extends GopayBaseJob {

    @Autowired
    private CustUniqIdenRelService custUniqIdenRelService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    protected void doJobInternal(){
        logger.info("生成唯一标识任务开始执行");
        List<CustPersonInfo>resultList = custUniqIdenRelService.showAllIsnotRealNameCertifyToday();
        if(resultList != null && !resultList.isEmpty()){
            JSONArray json1 = JSONArray.fromObject(resultList);
            String str = json1.toString();//把json转换为String
            logger.info("即将资质过期的对象："+str);
            for(CustPersonInfo info : resultList){
                logger.info("需要生成唯一标识的客户ID为："+info.getCustId());
                Integer i = custUniqIdenRelService.findByCustIdAndCertNo(info.getCertNo(),info.getCustId());
                CustUniqIdenRel idenRel = new CustUniqIdenRel();
                idenRel.setIdNumber(info.getCertNo());
                idenRel.setRegisTs(new Date());
                idenRel.setBusinessLicenceNo("");
                idenRel.setCustType("2");
                idenRel.setCreatedTs(new Date());
                idenRel.setLastUpdTs(new Date());
                idenRel.setCustId(info.getCustId());
                idenRel.setCustName(info.getCustName());
                if(i ==0){
                    String uuid = UUID.randomUUID().toString(); //获取UUID并转化为String对象
                    uuid = uuid.replace("-", "");
                    logger.info("新生成的唯一标识为："+uuid);
                    idenRel.setUniqIden(uuid);
                }else if(i>0){
                    String uniqIden = custUniqIdenRelService.showUniqIndeInfoByCertNoAndCustId(info.getCertNo(),info.getCustId());
                    logger.info("已经存在的唯一标识为："+uniqIden);
                    idenRel.setUniqIden(uniqIden);
                }
                try{
                    custUniqIdenRelService.saveCustuniqIdenRelInfo(idenRel);
                }catch (Exception e){
                    logger.error("任务出现异常："+e);
                }
            }
        }else{
            logger.info("无需执行生成唯一标识任务");
        }
    }
}
